package org.thinkinbunny.financial.view;

import org.thinkinbunny.financial.R;
import org.thinkinbunny.financial.billentity.BillEntiry;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
	private final LayoutInflater mInflater;
	private final Cursor mCursor;
	private final String[] sourceStrings;

	public ListViewAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {

		super();
		// TODO Auto-generated constructor stub
		mInflater = LayoutInflater.from(context);
		sourceStrings = from;
		mCursor = c;

	}

	// @Override
	// public View newView(Context context, Cursor cursor, ViewGroup parent) {
	// // TODO Auto-generated method stub
	//
	// return super.newView(context, cursor, parent);
	// }
	// @Override
	// public void bindView(View view, Context context, Cursor cursor) {
	// // TODO Auto-generated method stub
	// TextView billMoney=(TextView) view.findViewById(R.id.billMoneyTextView);
	// TextView billType=(TextView) view.findViewById(R.id.billTypeTextView);
	// TextView billTime=(TextView) view.findViewById(R.id.billTimeTextView);
	// //view.setTag(cursor.getString(0));
	// super.bindView(view, context, cursor);
	// }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCursor.getCount();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_items, null);
			viewHolder.billMoney = (TextView) convertView
					.findViewById(R.id.billMoneyTextView);
			viewHolder.billType = (TextView) convertView
					.findViewById(R.id.billTypeTextView);
			viewHolder.billTime = (TextView) convertView
					.findViewById(R.id.billTimeTextView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();

		}
		mCursor.moveToPosition(position);
		viewHolder.billMoney.setText(mCursor.getString(mCursor
				.getColumnIndex(sourceStrings[0])) + "￥ ");
		viewHolder.billType.setText(mCursor.getString(mCursor
				.getColumnIndex(sourceStrings[1])));

		viewHolder.billTime.setText(mCursor.getString(mCursor
				.getColumnIndex(sourceStrings[2]))
				+ "\n"
				+ mCursor.getString(mCursor.getColumnIndex(sourceStrings[3])));

		return convertView;
	}

	static class ViewHolder {
		private TextView billMoney;
		private TextView billType;
		private TextView billTime;

	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		mCursor.moveToPosition(position);
		float balence = mCursor.getFloat(mCursor.getColumnIndex("balance"));
		BillEntiry billEntiry = new BillEntiry(mCursor.getInt(mCursor
				.getColumnIndex("_id")), balence, mCursor.getString(mCursor
				.getColumnIndex("billType")), mCursor.getString(mCursor
				.getColumnIndex("accountname")), mCursor.getString(mCursor
				.getColumnIndex("project")), mCursor.getString(mCursor
				.getColumnIndex("store")), mCursor.getString(mCursor
				.getColumnIndex("date")), mCursor.getString(mCursor
				.getColumnIndex("time")));

		// billEntiry.createBillEntiry(mCursor.getFloat(mCursor.getColumnIndex("balance")),
		// mCursor.getString(mCursor.getColumnIndex("billType")),
		// mCursor.getString(mCursor.getColumnIndex("accountname")),
		// mCursor.getString(mCursor.getColumnIndex("project")),
		// mCursor.getString(mCursor.getColumnIndex("store")),
		// mCursor.getString(mCursor.getColumnIndex("date")),mCursor.getString(mCursor.getColumnIndex("time"))
		// );
		mCursor.moveToPrevious();
		return billEntiry;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

}
